<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :page="page"
      v-model="form"
      :option="option"
      :table-loading="loading"
      :data="data"
      @search-reset="searchReset"
      @current-change="currentChange"
      @search-change="searchChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" plain icon="el-icon-download" @click="handleExport">导出 </el-button>
      </template>
      <template slot-scope="{ row, index }" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" @click="handleDetail(row)">详情</el-button>
      </template>
    </avue-crud>
    <el-dialog v-dragDirectives ref="dialog" title="详情" append-to-body :visible.sync="detailDialog" :close-on-click-modal="false" top="5vh">
      <avue-form ref="basicInfo" v-if="detailDialog" v-model="detailDialogForm" :option="detailOption">
        <template slot="info">
          <avue-crud
            ref="infoCrud"
            :option="infoOption"
            :page="detailPage"
            :table-loading="detailLoading"
            @current-change="currentDetailChange"
            @size-change="sizeDetailChange"
            :data="infoData"
          >
          </avue-crud>
        </template>
      </avue-form>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-product/stat-production-execute/page" :option="option" :page="page" :query="form" file-name="生产执行报表"></export-comp>
  </basic-container>
</template>

<script>
import { getList, getDetailPage } from '@/api/report/producExecuteReport';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp';
export default {
  name: 'producExecuteReport',
  components: {
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'PRODUCT_EXECUTE_REPORT',
      loading: false,
      data: [],
      form: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      detailPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      detailLoading: false,
      exprotVisible: false,
      exportData: [],
      detailDialogForm: {},
      detailDialog: false,
      option: {
        height: 'calc(100vh - 540px)',
        border: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        index: true,
        menu: true,
        menuWidth: 100,
        columnBtn: false,
        refreshBtn: false,
        searchMenuSpan: 6,
        searchMenuPosition: 'right',
        indexLabel: '序号',
        column: [
          {
            label: '车间',
            prop: 'workshopName',
            search: true,
            span: 6
          },
          {
            label: '产线',
            prop: 'lineName',
            search: true,
            span: 6
          },
          {
            label: '日期',
            prop: 'eventDate',
            search: true,
            searchType: 'daterange',
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期',
            span: 8
          },
          {
            label: '计划工单数',
            prop: 'workOrderCount'
          },
          {
            label: '计划数量',
            prop: 'planQuantity'
          },
          {
            label: '计划工时',
            prop: 'planWorkHour'
          },
          {
            label: '实际完成数量',
            prop: 'actualQuantity'
          },
          {
            label: '有效工时',
            prop: 'validWorkHour',
            minWidth: 110
          },
          {
            label: '无效工时',
            prop: 'invalidWorkHour'
          },
          {
            label: '产线总工时',
            prop: 'totalWorkHour'
          }
        ]
      },
      detailOption: {
        submitBtn: false,
        emptyBtn: false,
        detail: true,
        group: [
          {
            label: '基础数据',
            arrow: false,
            prop: 'baseInfo',
            labelWidth: 120,
            display: true,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                label: '车间',
                prop: 'workshopName',
                span: 8
              },
              {
                label: '产线',
                prop: 'lineName',
                span: 8
              },
              {
                label: '日期',
                prop: 'eventDate',
                span: 8
              },
              {
                label: '计划工单数',
                prop: 'workOrderCount',
                span: 8
              },
              {
                label: '计划数量',
                prop: 'planQuantity',
                span: 8
              },
              {
                label: '计划工时',
                prop: 'planWorkHour',
                span: 8
              },
              {
                label: '实际完成数量',
                prop: 'actualQuantity',
                span: 8
              },
              {
                label: '有效工时',
                prop: 'validWorkHour',
                span: 8
              },
              {
                label: '无效工时',
                prop: 'invalidWorkHour',
                span: 8
              },
              {
                label: '产线总工时',
                prop: 'totalWorkHour',
                span: 8
              }
            ]
          },
          {
            label: '工单详情',
            arrow: false,
            prop: 'workOderInfo',
            labelWidth: 120,
            display: true,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                labelWidth: 0,
                label: '',
                prop: 'info',
                span: 24,
                hide: true,
                formslot: true
              }
            ]
          }
        ]
      },
      infoData: [],
      infoOption: {
        selection: false,
        editBtn: false,
        viewBtn: false,
        height: 270,
        border: true,
        menuWidth: 200,
        addBtn: false,
        delBtn: false,
        menu: false,
        tip: false,
        index: true,
        indexLabel: '序号',
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo'
          },
          {
            label: '客户数据',
            prop: 'customerCode'
          },
          {
            label: '客户名称',
            prop: 'customerName'
          },
          {
            label: '产品名称',
            prop: 'productPartNo'
          },
          {
            label: '产品型号',
            prop: 'productPartModel'
          },
          {
            label: '计划数量',
            prop: 'planQuantity'
          },
          {
            label: '计划工时',
            prop: 'planWorkHour'
          },
          {
            label: '完工数量',
            prop: 'actualQuantity'
          },
          {
            label: '不良数',
            prop: 'ngQuantity'
          },
          {
            label: '无效工时',
            prop: 'invalidWorkHour'
          },
          {
            label: '产线总工时',
            prop: 'totalWorkHour'
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      //搜索与表格结构分隔
      this.$structuralSplit();
    });
  },
  methods: {
    handleDetail(row) {
      this.detailDialogForm = row;
      this.onDetailLoad(this.detailPage);
      this.detailDialog = true;
    },
    async onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.form)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    async onDetailLoad(page, params = {}) {
      this.detailLoading = true;
      params.lineId = this.detailDialogForm.lineId;
      params.eventDate = this.detailDialogForm.eventDate;
      getDetailPage(page.currentPage, page.pageSize, Object.assign(params)).then((res) => {
        const data = res.data.data;
        this.detailPage.total = data.total;
        this.infoData = data.records;
        this.detailLoading = false;
      });
    },
    searchReset() {
      this.form = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.eventDate) {
        params.startTime = params.eventDate[0];
        params.endTime = params.eventDate[1];
        delete params.eventDate;
      }
      this.form = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad();
    },
    currentDetailChange(currentPage) {
      this.detailPage.currentPage = currentPage;
      this.onDetailLoad(this.detailPage);
    },
    sizeDetailChange(pageSize) {
      this.detailPage.pageSize = pageSize;
      this.onDetailLoad(this.detailPage);
    },
    handleExport() {
      this.$refs.exportComp.visible = true;
    },
    getDefaultTime() {
      return [this.$moment(new Date()).subtract(6, 'days').format('yyyy-MM-DD'), this.$moment(new Date()).format('yyyy-MM-DD')];
    }
  }
};
</script>

<style lang="scss" scoped>
.tab-font {
  ::v-deep .el-tabs__item {
    color: #606266 !important;
    font-weight: bold;
  }
}
::v-deep .el-dialog .avue-crud__menu {
  display: none;
}
</style>
